package com.cobla.struct.queue;

import com.cobla.struct.common.Const;
import lombok.Data;

/**
 * 顺序队列
 */
@Data
public class LinearQueue {
    private int[] data;
    private int front;
    private int rear;

    public LinearQueue() {
        this.data = new int[Const.MAXSIZE];
        this.rear = this.front = 0;
    }

    /**
     * 队列是否为空
     * @return
     */
    public boolean isEmpty() {
        return (this.front == this.rear);
    }

    /**
     * 入队
     * @param x
     * @return
     */
    public int enQueue(int x) {
        if ((this.rear + 1) % Const.MAXSIZE == this.front) {
            return 0;
        }
        this.rear = (this.rear + 1)%Const.MAXSIZE;
        this.data[this.rear] = x;
        return 1;
    }

    /**
     * 出队
     * @return 队空返回 null
     */
    public Integer deQueue() {
        if (this.rear == this.front) {
            return null;
        }
        this.front = (this.front + 1) % Const.MAXSIZE;
        return this.data[front];
    }
}
